/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: xsocket.h
 *
 * Purpose: wrapper socket operation API
 *
 * Developer:
 *   wen.gu , 2023-07-10
 *
 * TODO:
 *
 ***************************************************************************/

#ifndef __XBUS_OSAL_XSOCKET_H__
#define __XBUS_OSAL_XSOCKET_H__

#include <cstdint>
#include <string>
#include <functional>
#include <map>

#include "xbus/core/xtype.h"
#include "xbus/core/xerror.h"
#include "xbus/core/xresult.h"
#include "xbus/core/xbuffer.h"

#define XSOCKET_DOMAIN_


namespace xbus::osal {

enum class XSocketType: uint8_t {
    UDS = 0,
    TCP,
    UDP
};

using XSocketId = uint64_t;
using XSocketHandle = uint64_t;

struct XSocketPacket {
    uint32_t size;
    uint8_t* buf;
    XSocketId src_id;
    XSocketId dst_id;
};

enum class XSocketCmd: uint32_t {
    None = 0,
};

struct XSocketCmdParam {
    XSocketCmd cmd_;
    uint32_t size_;
    XSocketCmdParam(XSocketCmd cmd, uint32_t size):cmd_(cmd), size_(size) { }
};

template<typename SubType, XSocketCmd cmd>
struct XSocketCmdParamImpl: public XSocketCmdParam {
    XSocketCmdParamImpl(): XSocketCmdParam(cmd, sizeof(SubType)) { }
};




#if 0
core::XComId XSocketOpen(XSocketType type);
core::XErrC XSocketClose(core::XComId sock_fd);

/** for client side */
core::XErrC XSocketConnect(core::XComId sock_fd, const std::string& url, int32_t port = 0);

/** for server side */
core::XErrC XSocketBind(core::XComId sock_fd, const std::string& url, int32_t port = 0);
core::XComId XSocketAccept(core::XComId sock_fd, int32_t timeout_ms = core::WaitType::WaitForever); /** for orainted-connection protocol(e.g. tcp, uds) */


int32_t XSocketRecv(core::XComId sock_fd, uint8_t* buf, uint32_t size, int32_t timeout_ms = core::WaitType::WaitForever);
int32_t XSocketSend(core::XComId sock_fd, const uint8_t* buf, uint32_t size);

int32_t XSocketRecvFrom(core::XComId sock_fd, core::XComId & src_fd, uint8_t* buf, uint32_t size, int32_t timeout_ms = core::WaitType::WaitForever);
int32_t XSocketSendTo(core::XComId sock_fd, core::XComId dst_fd, const uint8_t* buf, uint32_t size);  


/** for udp */
core::XComId XSocketMakeDstComId(const std::string& url, int32_t port);


#else
/////////////////////////////////////////////////////////////////////////////////////////

core::XResult<XSocketHandle> XSocketOpen(XSocketType type);
core::XResultCode XSocketClose(XSocketHandle sock_hd);

/** for client side */
core::XResultCode XSocketConnect(XSocketHandle sock_hd, const std::string& url, int32_t port = 0);

/** for server side */
core::XResultCode XSocketBind(XSocketHandle sock_hd, const std::string& url, int32_t port = 0);
core::XResult<XSocketId> XSocketAccept(XSocketHandle sock_hd, int32_t timeout_ms = core::WaitType::WaitForever); /** for orainted-connection protocol(e.g. tcp, uds) */


core::XResult<uint32_t> XSocketRecv(XSocketHandle sock_hd, uint8_t* buf, uint32_t size, int32_t timeout_ms = core::WaitType::WaitForever);
core::XResultCode XSocketSend(XSocketHandle sock_hd, const uint8_t* buf, uint32_t size);

core::XResult<uint32_t> XSocketRecvFrom(XSocketHandle sock_hd, XSocketId& src_fd, uint8_t* buf, uint32_t size, int32_t timeout_ms = core::WaitType::WaitForever);
core::XResultCode XSocketSendTo(XSocketHandle sock_hd, XSocketId dst_fd, const uint8_t* buf, uint32_t size);


core::XResultCode XSocketSetOpt(XSocketHandle sock_hd, const XSocketCmdParam& param);
core::XResultCode XSocketGetOpt(XSocketHandle sock_hd, XSocketCmdParam& param);

/** for udp */
XSocketId XSocketMakeDstScoketId(const std::string& url, int32_t port);

#endif





class XSocket;

using XSocketSP = std::shared_ptr<XSocket>;

class XSocket {
public:
    virtual ~XSocket() = default;
public:
    virtual core::XResultCode bind(const std::string& url, int32_t port) = 0;
    virtual core::XResultCode connect(const std::string& url, int32_t port) = 0;

public:
    virtual core::XResult<core::XBufferPtr> recv(uint32_t req_size, int32_t timeout_ms) = 0;
    virtual core::XResultCode send(core::XComId dst_id, core::XBufferPtr data) = 0;
public:
    virtual core::XResult<core::XBufferPtr> alloc(uint32_t size) = 0;
};

class XSocketManager final {
public:
    using XSocketFactoryHandler = std::function<XSocketSP(const std::string& url, int32_t port)>;
    using XSocketMap = std::map<std::string, XSocketFactoryHandler>;
public:
    ~XSocketManager();
    static XSocketManager& getInstance();
protected:
    XSocketManager();
public:
    XSocketSP find(const std::string& protocol);

public:
    bool registSocket(const std::string& protocol, XSocketFactoryHandler);

private:
    XSocketMap socket_maps_;

};




} //namespace xbus::osal


#endif /** !__XBUS_OSAL_XSOCKET_H__ */